package com.css.apps.base.common.sync.client;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.caucho.hessian.client.HessianProxyFactory;
import com.css.apps.base.common.hessian.HessianConfig;
import com.css.apps.base.common.hessian.model.HessianServiceInfo;
import com.css.apps.base.common.sync.client.service.SyncService;
import com.css.util.ListUtil;

public class SyncClient {
	
	public List<SyncService> getServices(String syncId) {
		List<String> urls = null;
		Map<String, HessianServiceInfo> hessianService = HessianConfig.INSTANCE.getServices();
		if(hessianService != null && !hessianService.isEmpty()) {
			HessianServiceInfo hs = hessianService.get(syncId);
			if(hs == null) {
				throw new RuntimeException("SyncId is Not Found");
			}
			urls = hs.getServiceUrls();
		}
		if(!ListUtil.isLstEmpty(urls)) {
			List<SyncService> services = new ArrayList<>();
			for (String url : urls) {
				HessianProxyFactory factory = new HessianProxyFactory();
				factory.setOverloadEnabled(true);
				try {
					SyncService service = (SyncService) factory.create(SyncService.class, url);
					services.add(service);
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
			}
			return services;
		}
		return null;
	}
	
}
